package dao.Impl;


import dao.AdminsDao;
import entity.Admins;
import handler.CustomBeanListHandler;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import utils.C3p0Helper;

import java.sql.SQLException;
import java.util.List;

public class AdminsDaoImpl implements AdminsDao {
    @Override
    public List<Admins> findAll() {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from admins";
        try {
            return qr.query(sql, new CustomBeanListHandler());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Admins findById(int id) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from admins where id=?";
        ResultSetHandler<Admins> setHandler = new BeanHandler<>(Admins.class);
        try {
            return qr.query(sql,setHandler,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void save(Admins admin) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "insert into admins(adminName,adminPwd,realName,telephone,email,birthday,sex,pic,remark) values(?,?,?,?,?,?,?,?,?)";
        try {
            qr.update(sql,admin.getAdminName(),admin.getAdminPwd(),admin.getRealName(),admin.getTelephone(),admin.getEmail(),admin.getBirthday(),admin.getSex(),admin.getPic(),admin.getRemark());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(Admins admin) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "update admins set adminName=?,adminPwd=?,realName=?,telephone=?,email=?,birthday=?,sex=?,pic=?,remark=? where id=?";
        try {
            qr.update(sql,admin.getAdminName(),admin.getAdminPwd(),admin.getRealName(),admin.getTelephone(),admin.getEmail(),admin.getBirthday(),admin.getSex(),admin.getPic(),admin.getRemark(),admin.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(int id) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "delete from admins where id=?";
        try {
            qr.update(sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
    @Override
    public List<Admins> findByName(String realName) {
        QueryRunner runner = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from admins where realName like ?";
        try {
            return runner.query(sql,new CustomBeanListHandler(),"%"+realName+"%");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public Admins login(String name,String password) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from admins where adminName=? and adminPwd=?";
        ResultSetHandler<Admins> setHandler = new BeanHandler<>(Admins.class);
        try {
            return qr.query(sql,setHandler,name,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
